package com.zhny.aiot.top.iot.insect.pest.plugin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.topny.TopYNConfig;
import com.zhny.aiot.plugin.topny.TopYNWebDevice;
import com.zhny.aiot.plugin.topny.api.ApiResponse;
import com.zhny.aiot.top.iot.insect.pest.plugin.model.InsetPestStoreEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class InsectPestDevice extends TopYNWebDevice {
    private static final Logger log=LoggerFactory.getLogger (InsectPestDevice.class);
    private final Map<String, String> CommandOperations;
    public InsectPestDevice(IDeviceFactory<TopYNConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super (factory, centerFacilities, entity);
        CommandOperations =  new ImmutableMap.Builder<String, String>()
                .put("5", "玉米螟").put("6", "大螟").put("7", "二化螟").put("8", "稻纵卷叶螟")
                .put("9", "白背飞虱").put("10", "褐飞虱属").put("25", "地老虎").put("26", "铜绿丽金龟")
                .put("41", "蝼蛄").put("43", "桃蛀螟").put("44", "斜纹夜蛾").put("95", "大青叶蝉")
                .put("105", "粘虫").put("110", "草地螟").put("115", "甜菜夜蛾").put("148", "黄足猎蝽")
                .put("156", "八点灰灯蛾").put("175", "蚱蜢").put("213", "竹大黄").put("222", "棉铃虫")
                .put("228", "二点委夜蛾").put("233", "美国白蛾").put("235", "甘蓝夜蛾").put("236", "银纹夜蛾")
                .put("256", "蟋蟀").put("276", "白毒蛾").put("279", "青尺蛾").put("280", "黄毒蛾")
                .put("292", "灰飞虱").put("293", "黑尾叶蝉").put("310", "稻螟蛉").put("321", "草地贪夜蛾")
                .put("327", "稻水象甲").put("338", "松毛虫属").put("344", "金纹细蛾").put("369", "竹舟蛾")
                .put("381", "竹螟").put("382", "姬尺蛾").put("383", "金黄镰翅野螟").put("384", "卷叶蛾科")
                .put("385", "星白雪灯蛾").put("386", "金星尺蛾").put("387", "紫条尺蛾").put("389", "绿刺蛾")
                .put("392", "水螟蛾").put("393", "小菜蛾").put("394", "线委夜蛾").put("395", "黄刺蛾")
                .put("397", "扁刺蛾").put("398", "甜菜白带野螟").put("399", "标瑙夜蛾").put("401", "歧角蛾")
                .put("402", "瓜绢野螟").put("404", "槐尺蛾").put("408", "美苔蛾").put("409", "草雪苔蛾")
                .put("415", "黄苔蛾").put("417", "黄苔蛾").put("421", "连斑水螟").put("424", "稻苞虫")
                .put("430", "豆荚野螟蛾").put("473", "尺蛾科").put("478", "黑纹黄灯蛾").put("479", "黄苔蛾")
                .put("480", "石蛾").put("484", "白条夜蛾").put("485", "大黑鳃金龟").put("486", "烟青虫")
                .put("487", "棉大卷叶螟").put("508", "灰胸突鳃金龟").put("510", "褛裳夜蛾").put("533", "杨扇舟蛾")
                .put("550", "乏夜蛾").put("556", "旋歧夜蛾").put("564", "宽胫夜蛾").put("567", "大造桥虫")
                .put("569", "谐夜蛾").put("574", "白肾俚夜蛾").put("579", "杨小舟蛾").put("588", "豆荚斑螟")
                .put("595", "黄杨缀叶野螟").put("598", "尖锥额野螟").put("660", "亚麻篱灯蛾").put("669", "朽木夜蛾")
                .put("672", "豹灯蛾").put("673", "干纹冬夜蛾").put("688", "榆黄足毒蛾").put("698", "埋葬虫")
                .put("729", "柳蓝叶甲").put("740", "珀蝽").put("743", "姬蜂").put("745", "中华真地鳖")
                .put("762", "缕裳夜蛾").put("825", "稻飞虱").put("826", "稻纵卷叶螟").put("827", "粘虫")
                .put("843", "负泥虫").put("844", "稻摇蚊").put("845", "潜叶蝇").put("850", "金龟科")
                .put("1029", "淡色库蚊（雌）").put("1030", "淡色库蚊（雄）").put("1031", "白纹伊蚊（雌）").put("1032", "白纹伊蚊（雄）")
                .put("1034", "四斑绢野螟").put("1035", "豆卷叶螟").put("1036", "细条纹野螟").put("1037", "小绿叶蝉属")
                .put("1046", "电光叶蝉").put("1048", "褐角庶尺蛾").put("1050", "灯蛾").put("1051", "稻巢草螟")
                .put("1052", "台湾稻螟").put("1053", "豆毒蛾").put("1054", "格庶尺蛾").put("1055", "埋葬甲")
                .put("1056", "楸蠹野螟").put("1057", "中带三角夜蛾").put("1058", "豆天蛾").put("1059", "银辉夜蛾")
                .put("1060", "中华晓扁犀金龟").put("1061", "黄边美苔蛾").put("1062", "野蚕蛾").put("1063", "桑螟")
                .build();
    }

    @Override
    protected String termType() {
        return "1";
    }

    @Override
    protected void onResponse(ApiResponse apiResponse) {
        if(apiResponse == null)
            return;
        if(!apiResponse.getCode ().equals ("0"))
            return;
        if(apiResponse.getData ().size () == 0)
            return;
        JSONArray list =(JSONArray) apiResponse.getData ().get ("list");
        if(list == null || list.size () == 0)
            return;
        JSONArray imgList = (JSONArray)((JSONObject)list.get (0)).get ("img_list");
        if(imgList == null || imgList.size () == 0)
            return;
        JSONObject img =(JSONObject)imgList.get (0);
        String imgUrl =(String) (img).get ("img_url");
        JSONArray insectList =(JSONArray) img.get ("insect");
        StringBuilder strBuilder = new StringBuilder ();
        if(insectList != null && insectList.size () > 0){
            insectList.forEach (i->{
                JSONObject insect = (JSONObject)i;
                if(CommandOperations.containsKey (insect.get ("insect_type_id"))) {
                    strBuilder.append ("虫类:");
                    strBuilder.append (CommandOperations.get (insect.get ("insect_type_id")));
                    strBuilder.append ("数量：");
                    strBuilder.append (insect.get ("number"));
                    strBuilder.append (";");
                }
            });
        }
        InsetPestStoreEntry entry = new InsetPestStoreEntry ();
        entry.setImgUrl (imgUrl);
        entry.setResult (strBuilder.toString ());
        save (entry);
        saveReal (entry);
    }
}
